package com.danciyixue.boot.dcyx.controller;

import com.danciyixue.boot.dcyx.service.TestsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.danciyixue.boot.dcyx.model.form.TestsForm;
import com.danciyixue.boot.dcyx.model.query.TestsQuery;
import com.danciyixue.boot.dcyx.model.vo.TestsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 测试试卷信息前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:21
 */
@Tag(name = "测试试卷信息接口")
@RestController
@RequestMapping("/api/v1/tests")
@RequiredArgsConstructor
public class TestsController  {

    private final TestsService testsService;

    @Operation(summary = "测试试卷信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('dcyx:tests:query')")
    public PageResult<TestsVO> getTestsPage(TestsQuery queryParams ) {
        IPage<TestsVO> result = testsService.getTestsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增测试试卷信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('dcyx:tests:add')")
    public Result<Void> saveTests(@RequestBody @Valid TestsForm formData ) {
        boolean result = testsService.saveTests(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取测试试卷信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('dcyx:tests:edit')")
    public Result<TestsForm> getTestsForm(
        @Parameter(description = "测试试卷信息ID") @PathVariable Long id
    ) {
        TestsForm formData = testsService.getTestsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改测试试卷信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('dcyx:tests:edit')")
    public Result<Void> updateTests(
            @Parameter(description = "测试试卷信息ID") @PathVariable Long id,
            @RequestBody @Validated TestsForm formData
    ) {
        boolean result = testsService.updateTests(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除测试试卷信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('dcyx:tests:delete')")
    public Result<Void> deleteTestss(
        @Parameter(description = "测试试卷信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = testsService.deleteTestss(ids);
        return Result.judge(result);
    }
}
